package erp_services

import (
	"errors"
	"xiehui-service/app/models/erp_models"
	"xiehui-service/global"
)

type productionLineService struct {
}

var ProductionLineService = new(productionLineService)

// 新增
func (productionLineService *productionLineService) Create(pl erp_models.ProductionLine) (uint, error) {
	result := global.App.DB.Create(&pl)
	if result.Error != nil {
		return 0, result.Error
	}
	return pl.ID.ID, nil
}

func (productionLineService *productionLineService) Update(pl erp_models.ProductionLine) (err error) {
	if err := global.App.DB.Model(&erp_models.ProductionLine{}).Where("ID = ?", pl.ID.ID).Updates(&pl).Error; err != nil {
		return err
	}
	return nil
}

func (productionLineService *productionLineService) GetByProductionNo(production_no string) (pl *erp_models.ProductionLine, err error) {
	err = global.App.DB.Where("production_no = ?", production_no).First(&pl).Error
	if err != nil || pl.ID.ID == 0 {
		if err.Error() == "record not found" {
			err = errors.New("数据不存在")
		}
	}
	return
}
